<style lang="less">
    @import './style.less';
</style>

<template>
    <div class="classification">
        <tree-ex
            treeTitle="分类管理"
            :data="classifis"
            :props='defaultProps'
            :operations="operations"
            :default-expanded-keys="defaultExpandedKeys"
            :onNodeClick="nodeClick"
            :onOperation="operationClick"/>
        <dialog-ex  
            v-if="!!dialogModal"
            :title="this.dialogModal === 'add' ? '新增分类' : '编辑分类'"
            :form="form"
            :mode="dialogModal"
            :init="currentKnowledge"/>
    </div>
</template>

<script>
    import { mapGetters, mapActions, mapMutations } from 'vuex'
    import { Message } from 'element-ui'
    import pageConfig from './config'
    import treeEx from '@/components/tree/treeEx'
    import dialogEx from '@/components/dialog/dialogEx'

    export default {
        name: 'classification',
        components: {
            'tree-ex': treeEx,
            'dialog-ex': dialogEx
        },
        data() {
            return {
                operations: pageConfig.operations,
                dialogModal: '',
                defaultExpandedKeys: [],
                currentKnowledge: null,
                defaultProps: {
                    children: 'subClasses',
                    label: 'name'
                },
                form: {
                    items: pageConfig.formItems,
                    actions: {
                        cancel: () => {
                            this.dialogModal = ''
                        },
                        confirm: (data, mode) => {
                            this.dialogModal = ''
                            if (this.currentKnowledge) {
                                this.defaultExpandedKeys = [this.currentKnowledge.id]
                            }
                            if (mode === 'add') {
                                let params = {
                                    classParentId: this.currentKnowledge ? this.currentKnowledge.id : '0',
                                    name: data.name,
                                    classType: this.currentKnowledge ? this.currentKnowledge.classType : 'key_class_knowledge',
                                    enabled: data.enabled,
                                    homeShow: data.homeShow,
                                    sequence: data.sequence
                                }
                                this.editKnowledge(params)
                            }
                            if (mode === 'edit') {
                                let params = {
                                    id: this.currentKnowledge.id,
                                    name: data.name,
                                    enabled: data.enabled,
                                    homeShow: data.homeShow,
                                    sequence: data.sequence
                                }
                                this.editKnowledge(params)
                            }
                        }
                    }
                }
            }
        },

        /**
         * 生命钩子
         **/
        created() {
            this.loadData()
        },
        mounted() {},

        /**
         * 计算
         **/
        computed: {
            ...mapGetters({
                classifis: 'classifis',
                classifisOperations: 'classifisOperations'
            })
        },
        /**
         * 监控
         **/
        watch: {},

        /**
         * 事件
         **/
        methods: {

            ...mapActions([
                'getKnowledge',
                'editKnowledge',
                'removeKnowledge'
            ]),

            loadData() {
                this.getKnowledge()
            },

            nodeClick(e) {
                this.currentKnowledge = e
            },

            operationClick(oper) {
                let prop = oper.prop
                switch(prop) {
                    case 'refresh':
                        this.defaultExpandedKeys = []
                        this.getKnowledge()
                        break
                    case 'add':
                        this.dialogModal = 'add'
                        break
                    case 'edit':
                        this.dialogModal = 'edit'
                        break
                    default:
                        console.log(this.currentKnowledge)
                        let params = {
                            id: this.currentKnowledge.id
                        }
                        this.removeKnowledge(params)
                }
            }

        },
    }
</script>

